﻿using Autofac;
using CarShop.Data.EntityFramework;
using CarShop.Data.EntityFramework.Interfaces;
using CarShop.Data.EntityFramework.Repositories;
using CarShop.Domain.Interfaces;
using CarShop.Domain.Interfaces.Repositories;
using CarShop.Security;
using CarShop.Security.Contracts;
using CarShop.Services;
using CarShop.Services.Contracts.Interfaces;
using CarShop.UserInterface.Wpf.Core.Infrastructure;
using CarShop.UserInterface.Wpf.ViewModels;

namespace CarShop.UserInterface.Wpf.Infrastructure
{
    public static class IoCContainer
    {
        public static IContainer BaseContainer { get; private set; }

        public static void Build()
        {
            if (BaseContainer == null)
            {
                var builder = new ContainerBuilder();
                builder.RegisterType<CarShopContext>().As<IUnitOfWork>().As<IQueryableUnitOfWork>().InstancePerLifetimeScope();
                builder.RegisterType<UserRepository>().As<IUserRepository>();
                builder.RegisterType<RoleRepository>().As<IRoleRepository>();
                builder.RegisterType<CarRepository>().As<ICarRepository>();
                builder.RegisterType<CarStatusRepository>().As<ICarStatusRepository>();
                builder.RegisterType<CarTypeRepository>().As<ICarTypeRepository>();
                builder.RegisterType<CarBrandRepository>().As<ICarBrandRepository>();
                builder.RegisterType<OrderRepository>().As<IOrderRepository>();
                builder.RegisterType<OrderStatusRepository>().As<IOrderStatusRepository>();
                builder.RegisterType<PictureRepository>().As<IPictureRepository>();
                builder.RegisterType<UserInfoRepository>().As<IUserInfoRepository>();
                builder.RegisterType<CarService>().As<ICarService>();
                builder.RegisterType<OrderService>().As<IOrderService>();
                builder.RegisterType<CarShopMembership>().As<IMembershipProvider>();
                builder.RegisterType<AuthenticationService>().As<IAuthenticationService>();


                //ViewModels
                builder.RegisterType<AuthenticationWindowViewModel>();

                BaseContainer = builder.Build();
            }
        }

        public static TService Resolve<TService>()
        {
            return BaseContainer.Resolve<TService>();
        }
    }
}
